package com.altekis.kanbankaze.client;

import com.altekis.kanbankaze.client.ui.EditableLabel;
import com.altekis.kanbankaze.shared.KanbanProperties;
import com.altekis.kanbankaze.shared.Task;
import com.altekis.kanbankaze.shared.UserAccount;
import com.altekis.kanbankaze.shared.events.CardEditedEvent;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.Image;
import com.google.inject.Inject;

/**
 * Task on board presenter
 */
public class TaskCardPresenter {
	interface Display {
		void expandCollapse();

		EditableLabel getDescriptionField();

		HasClickHandlers getEditButton();

		EditableLabel getNameField();

		HasText getTitleField();

		void setPixelPosition(int x, int y);

		AbsolutePanel getAvatarPanel();
	}

	protected Display display;
	protected final EventBus eventBus;
	protected Task task;

	protected final TaskServiceAsync taskService;
	protected final UserAccountServiceAsync userAccountService;

	@Inject
	TaskCardPresenter(TaskServiceAsync taskService,
			UserAccountServiceAsync userAccountService, EventBus eventBus) {
		this.taskService = taskService;
		this.userAccountService = userAccountService;
		this.eventBus = eventBus;
	}

	void bindDisplay(final Display display) {
		this.display = display;

		display.getEditButton().addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				doEdit();
			}
		});

		display.getNameField().addValueChangeHandler(
				new ValueChangeHandler<String>() {

					@Override
					public void onValueChange(ValueChangeEvent<String> event) {
						TaskCardPresenter.this.task.setName(event.getValue());
						TaskCardPresenter.this.doSave();
					}
				});
		display.getDescriptionField().addValueChangeHandler(
				new ValueChangeHandler<String>() {

					@Override
					public void onValueChange(ValueChangeEvent<String> event) {
						TaskCardPresenter.this.task.setDescription(event
								.getValue());
						TaskCardPresenter.this.doSave();
					}
				});
	}
	
	/**
	 * Draws the avatar of the owner user
	 * @param usr
	 */
	private void drawAvatar(UserAccount usr) {
		Image img = new Image(usr.getAvatarUrl());
		img.getElement().setId("avatar");
		display.getAvatarPanel().add(img);
	}

	public void doEdit() {
		this.display.expandCollapse();
	}

	void doSave() {
		taskService.save(task, new AsyncCallback<Task>() {

			@Override
			public void onFailure(Throwable caught) {
				// TODO Handle exception
				caught.printStackTrace();
			}

			@Override
			public void onSuccess(Task result) {
				eventBus.fireEvent(new CardEditedEvent(task));
			}
		});
	}

	/**
	 * @return the task
	 */
	public Task getTask() {
		return task;
	}

	void refresh() {
		display.getNameField().setText(task.getName());
		display.getDescriptionField().setText(task.getDescription());
	}

	void showTask(Task taskToShow, KanbanProperties kanbanProperties) {
		task = taskToShow;
		display.getTitleField().setText("T" + task.getPriority());
		display.getNameField().setText(task.getName());
		display.getDescriptionField().setText(task.getDescription());
		
		userAccountService.findById(task.getOwner(), new AsyncCallback<UserAccount>() {

			@Override
			public void onSuccess(UserAccount result) {
				if (result != null
						&& result.getAvatarUrl()!=null && !"".equals(result.getAvatarUrl())) {
					drawAvatar(result);
				}
			}

			@Override
			public void onFailure(Throwable caught) {
				throw new RuntimeException(caught);
			}
		});
	}
}
